/*
 * File: TestUser.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package faceBook.tests.helper;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import servlets.ParseRequest;

import data.JavaCurl;
import faceBook.User;
import foxtrotbravo.Config;

/**
 * The Class TestUser.
 * 
 * @author Dude
 */
public class TestUsers
{

  /**
   * The Class testuser.
   */
  private static class testuser
  {

    /** The id. */
    public long id;

    /** The access token. */
    public String accessToken;

    /**
     * Instantiates a new testuser.
     */
    public testuser()
    {
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
      return id + "," + accessToken;
    }

  }

  /** The test users. */
  private static ArrayList<testuser> testUsers = null;

  /** The app access token. */
  private static String appAccessToken = null;

  /**
   * Get Test user.
   * 
   * @return the test user
   */
  public static User getTestUser()
  {
    return getTestUser(0);
  }

  /**
   * Get Test user.
   * 
   * @param index
   *          the index
   * @return the test user
   */
  public static User getTestUser(int index)
  {
    while (getTestUsers().size() - 1 < index)
    {
      addTestUser();
    }
    testuser tu = getTestUsers().get(index);
    User user = new User(tu.id);
    user.setAccessToken(tu.accessToken);
    user.save();
    return user;
  }

  /**
   * Adds the test user.
   */
  private static void addTestUser()
  {
    addTestUsers(1);
  }

  /**
   * Adds the test users.
   * 
   * @param count
   *          the count
   */
  private static void addTestUsers(int count)
  {
    String permissions = "publish_stream,read_stream,email,friends_birthday,user_birthday";
    for (int i = 0; i < count; i++)
    {
      JavaCurl.getUrl("https://graph.facebook.com/" + Config.AppId
          + "/accounts/test-users?installed=true&name=" + "New%20Hack"
          + "&locale=en_US&permissions=" + permissions
          + "&method=post&access_token=" + getAppAccessToken());
    }
    resetTestUsers();
    befriendAllUsers();
  }

  /**
   * Delete test user.
   * 
   * @param id
   *          the id
   */
  public static void deleteTestUser(long id)
  {
    JavaCurl.getUrl("https://graph.facebook.com/" + id
        + "?method=delete&access_token=" + getAppAccessToken());
    User user = new User(id);
    user.delete();
    resetTestUsers();
  }

  /**
   * Delete all test users.
   */
  public static void deleteAllTestUsers()
  {
    while(getTestUsers().size() > 0)
    {
      deleteTestUser(getTestUsers().get(0).id);
    }
  }

  /**
   * Befriend users.
   * 
   * @param index1
   *          the index1
   * @param index2
   *          the index2
   */
  private static void befriendUsers(int index1, int index2)
  {
    JavaCurl.asyncGetUrl("https://graph.facebook.com/"
        + getTestUser(index1).getID() + "/friends/"
        + getTestUser(index2).getID() + "?method=post&access_token="
        + getTestUser(index1).getAccessToken());
  }

  /**
   * Befriend all users.
   */
  public static void befriendAllUsers()
  {
    for (int i = 0; i < getTestUsers().size(); i++)
    {
      befriendUserWithAllUsers(i);
    }
  }

  /**
   * Befriend user with all users.
   * 
   * @param index
   *          the index
   */
  private static void befriendUserWithAllUsers(int index)
  {
    for (int i = 0; i < getTestUsers().size(); i++)
    {
      if (i != index)
        befriendUsers(i, index);
    }
  }

  /**
   * Reset test users.
   */
  private static void resetTestUsers()
  {
    testUsers = null;
  }

  /**
   * Get Test users.
   * 
   * @return the test users
   */
  private static ArrayList<testuser> getTestUsers()
  {
    if (testUsers == null)
    {
      testUsers = new ArrayList<testuser>();
      try
      {
        String testUsersResponse = JavaCurl
            .getUrl("https://graph.facebook.com/" + Config.AppId
                + "/accounts/test-users?access_token=" + getAppAccessToken());

        JSONObject jo;
        jo = new JSONObject(testUsersResponse);
        JSONArray ja = jo.getJSONArray("data");
        for (int i = 0; i < ja.length(); i++)
        {
          testuser newTestUser = new testuser();
          JSONObject user = ja.getJSONObject(i);
          newTestUser.id = user.getLong("id");
          newTestUser.accessToken = user.getString("access_token");
          ParseRequest.createUser(newTestUser.accessToken, newTestUser.id);
          testUsers.add(newTestUser);
        }
      } catch (JSONException e)
      {
        e.printStackTrace();
      }
    }
    return testUsers;
  }

  /**
   * Get App access token.
   * 
   * @return the app access token
   */
  private static String getAppAccessToken()
  {
    if (appAccessToken == null)
    {
      String tokenResponse = JavaCurl
          .getUrl("https://graph.facebook.com/oauth/access_token?client_id="
              + Config.AppId + "&client_secret=" + Config.AppSecret
              + "&grant_type=client_credentials");
      String[] tokenParts = tokenResponse.split("=");
      appAccessToken = tokenParts[1].trim();
    }
    return appAccessToken;
  }

}
